//
//  ClassifyTitleView.m
//  B2C
//
//  Created by xiaowu on 2017/7/27.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "ClassifyTitleView.h"
#import "RedPointButton.h"

static CGFloat itemWidth = 74;
static CGFloat itemWidthRed = 84;
static CGFloat itemHeight = 42;
static NSInteger TitleButtonTag = 1241232;

@interface ClassifyTitleView ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *borderLine;
@end

@implementation ClassifyTitleView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _titleArr = [NSArray array];
        
        [self addSubview:self.scrollView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _titleArr = [NSArray array];
        
        [self addSubview:self.scrollView];
        [self addSubview:self.borderLine];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    self.borderLine.frame = CGRectMake(self.width - 1, 0, 1, self.height);
}

// 通用方法
- (void)setupWithTitleStrsArr:(NSArray <NSString *> *)arr withIndex:(NSInteger)index {
    [self.scrollView removeAllSubviews];
    [self.scrollView addSubview:self.lineView];
    
    self.titleArr = arr;
    
    
    
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        NSString *titleStr = [self.titleArr safeObjectAtIndex:i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, i * itemHeight, itemWidth, itemHeight);
        
        [button setTitle:titleStr forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor ThemeColor] forState:UIControlStateSelected];
        button.titleLabel.font = TextPingFangFontSize(14);
        
        button.tag = TitleButtonTag + i;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor grayDDColor];
        lineView.frame = CGRectMake(0, 0, 37, 1);
        lineView.center = CGPointMake(button.center.x, button.height - lineView.height * 0.5);
        [button addSubview:lineView];
        [self.scrollView addSubview:button];
    }
    [self.scrollView bringSubviewToFront:self.lineView];

    self.scrollView.contentSize = CGSizeMake(self.width, itemHeight * self.titleArr.count);
    [self setIndexWithStrsArr:self.titleArr withShowIndex:index];
}

// 带红点 @{@"title":@"标题",@"number":@"数量"}
- (void)setupRedPointWithTitleArr:(NSArray<ClassifyTitleModel *> *)arr withIndex:(NSInteger)index {
    [self.scrollView removeAllSubviews];
    [self.scrollView addSubview:self.lineView];
    
    self.titleArr = arr;
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        ClassifyTitleModel *titleModel = [self.titleArr safeObjectAtIndex:i];
        
        RedPointButton *button = [RedPointButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, i * itemHeight, itemWidthRed, itemHeight);
        
        [button setRedPointText:titleModel.number];
        [button setTitle:titleModel.title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor ThemeColor] forState:UIControlStateSelected];
        button.titleLabel.font = TextPingFangFontSize(14);
        
        button.tag = TitleButtonTag + i;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor grayDDColor];
        lineView.frame = CGRectMake(0, 0, 37, 1);
        lineView.center = CGPointMake(button.center.x, button.height - lineView.height * 0.5);
        [button addSubview:lineView];
        [self.scrollView addSubview:button];
    }
    [self.scrollView bringSubviewToFront:self.lineView];
    
    self.scrollView.contentSize = CGSizeMake(0, itemHeight * self.titleArr.count);
    [self setIndexWithStrsArr:self.titleArr withShowIndex:index];
    
    //[self.borderLine setHidden:YES];
}

/**
 *  @brief str show index
 */
- (void)setIndexWithStrsArr:(NSArray *)arr withShowIndex:(NSInteger)index {
    self.titleArr = arr;
    self.currentIndex = index;
    
    UIButton *button = [self.scrollView viewWithTag:TitleButtonTag + index];
    button.titleLabel.font = TextPingFangFontSize(16);
    [button setSelected:YES];
    self.lineView.center = CGPointMake(self.lineView.width * 0.5, itemHeight * (index + 0.5));
}

- (void)setToShowIndex:(NSInteger)index withAnimation:(BOOL)need {
    self.currentIndex = index;
    
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            button.titleLabel.font = TextPingFangFontSize(14);
            [button setSelected:NO];
            if (button.tag == TitleButtonTag + index) {
                button.titleLabel.font = TextPingFangFontSize(16);
                [button setSelected:YES];
                [self animationForLine:button withAnimation:YES];
            }
        }
    }
    
}

- (void)buttonClick:(UIButton *)sender {
    NSInteger index = sender.tag - TitleButtonTag;
    self.currentIndex = index;
    [self animationForLine:sender withAnimation:YES];
    
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            button.titleLabel.font = TextPingFangFontSize(14);
            [button setSelected:NO];
        }
    }
    sender.titleLabel.font = TextPingFangFontSize(16);
    [sender setSelected:YES];
    
    if (self.block) {
        self.block(index);
    }
}

- (void)animationForLine:(UIButton *)sender withAnimation:(BOOL)animation {
    
    CGFloat offset = sender.bottom - self.scrollView.contentOffset.y;
    if (offset >= (self.scrollView.height - 50) && (sender.mj_y != 0)) {
        
        if (animation) {
            [self.scrollView scrollRectToVisible:CGRectMake(0, self.scrollView.contentOffset.y + offset - self.scrollView.height + 50, self.scrollView.width, self.scrollView.height) animated:YES];
        } else {
            [self.scrollView setMj_offsetY:(self.scrollView.contentOffset.y + offset - self.scrollView.height + 50)];
        }
    } else if ((offset >= 0) && (sender.mj_y == 0)) {
        if (animation) {
            [self.scrollView scrollRectToVisible:CGRectMake(0, 0, self.scrollView.width, self.scrollView.height) animated:YES];
        } else {
            [self.scrollView setMj_offsetY:0];
        }
    }
    
    
    CGFloat bottomOffset = self.scrollView.contentOffset.y - sender.top;
    if (bottomOffset >= -50 && (sender.mj_y != 0)) {
        if (animation) {
            [self.scrollView scrollRectToVisible:CGRectMake(0, self.scrollView.contentOffset.y - bottomOffset - 50, self.scrollView.width, self.scrollView.height) animated:YES];
        } else {
            [self.scrollView setMj_offsetY:(self.scrollView.contentOffset.y - bottomOffset - 50)];
        }
    }
    
    if (animation) {
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.lineView.center = CGPointMake(weakSelf.lineView.centerX, sender.centerY);
        } completion:^(BOOL finished) {
            
        }];
    } else {
        self.lineView.center = CGPointMake(self.lineView.centerX, sender.centerY);
    }
    
    return;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.lineView.center = CGPointMake(weakSelf.lineView.centerX, sender.centerY);
    } completion:^(BOOL finished) {
        
    }];
    
    
}

#pragma mark - getters
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor ThemeColor];
        _lineView.size = CGSizeMake(4, 24);
    }
    return _lineView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UIView *)borderLine {
    if (!_borderLine) {
        _borderLine = [[UIView alloc] init];
        _borderLine.backgroundColor = [UIColor grayDDColor];
    }
    return _borderLine;
}

@end
